package com.sjme.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjme.domain.user.User;
import com.sjme.domain.user.dto.UserDto;
import com.sjme.domain.user.vo.UserVo;
import com.sjme.mapper.UserMapper;
import com.sjme.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjme.AESUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.constant.RedisKey.User.INTERVIEW_PC_LOGIN_TOKEN;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LY
 * @since 2025-10-10
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private AESUtil aesUtil;

    @Override
    public IPage<User> queryUser(UserDto user) {
        Integer current = user.getCurrent() == null ? 1 : user.getCurrent();
        Integer size = user.getSize() == null ? 6 : user.getSize();
        // 基本查询条件
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotBlank(user.getName()), User::getName, user.getName());
        lqw.like(StringUtils.isNotBlank(user.getContactNumber()), User::getContactNumber, user.getContactNumber());
        // 分页查询条件
        IPage<User> page = new Page<>(current,size);
        return this.page(page, lqw);
    }

    @Override
    public List<User> queryUserByCondition(User user) {
        return userMapper.selectByCondition(user);
    }

    @Override
    public UserVo login(User user) {
        // 获取参数
        String account = user.getAccount();
        String password = user.getPassword();
        if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) {
            throw new RuntimeException("传入的账号或密码为空！");
        }
        // 校验账号
        User userInfo = this.getOne(Wrappers.<User>lambdaQuery().eq(User::getAccount, account));
        if (userInfo == null) {
            throw new RuntimeException("当前账号不存在！");
        }
        if (!password.equals(userInfo.getPassword())) {
            throw new RuntimeException("密码错误！");
        }
        // 将用户id进行对称加密生成token
        String token = aesUtil.encrypt(userInfo.getId());
        // 缓存key = interview:pc:login:token:用户id
        // 缓存value = token
        stringRedisTemplate.opsForValue().set(INTERVIEW_PC_LOGIN_TOKEN + userInfo.getId(),token,4, TimeUnit.HOURS);
        // 响应用户信息
        UserVo userVo = new UserVo();
        userVo.setToken(token);
        BeanUtils.copyProperties(userInfo, userVo);
        return userVo;
    }
}
